package translinkApp.Utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.text.ClipboardManager;

public class Utils {

	public static void openUrl(final Context context, final String url) {
		Intent urlIntent = new Intent(Intent.ACTION_VIEW);
		urlIntent.setData(Uri.parse(url));
		context.startActivity(urlIntent);
	}

	public static boolean isOnline(Context context) {
		boolean isConnected = false;
		try {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo info = cm.getActiveNetworkInfo();
			isConnected = info.isConnected();
		} catch (Exception e) {

			isConnected = false;
		}
		if (isConnected == false) {
			showAlertDialog(context, "You are not connected to the internet!");
		}

		return isConnected;
	}

	public static AlertDialog showDialog(Context context, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setCancelable(true)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	public static AlertDialog showAlertDialog(Context context, String message) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message).setCancelable(true)
				.setIcon(android.R.drawable.stat_sys_warning)
				.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						dialog.cancel();
					}
				});
		AlertDialog alert = builder.create();
		alert.show();

		return alert;
	}

	public static void setClipboardText(Context context, String text) {
		ClipboardManager ClipMan = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		ClipMan.setText(text);
	}

	public static void setClipboardText(Context context, CharSequence text) {
		ClipboardManager ClipMan = (ClipboardManager) context
				.getSystemService(Context.CLIPBOARD_SERVICE);
		ClipMan.setText(text.toString());
	}

	public static String inputStreamToString(InputStream is) throws IOException {

		BufferedReader r = new BufferedReader(new InputStreamReader(is));
		StringBuilder total = new StringBuilder();
		String line;
		while ((line = r.readLine()) != null) {
			total.append(line);
		}

		return total.toString();
	}
	

	public static String getLocationInformation(String locationInformation) {
		String locationString = locationInformation.replace("(Location)", "");
		locationString = locationString.replace("(Landmark)", "");
		locationString = locationString.replace("(Stop)", "");
		locationString = locationString.replace("(Suburb)", "");

		locationString = locationString.replace("opposite", "Opp");
		locationString = locationString.replace("approaching", "App");
		locationString = locationString.replace("far side of", "F/S");

		return locationString.trim();
	}

}